﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NRCP.BusinessLogicLayer;
using NRCP.BusinessEntities;

namespace NRCP.MetaData
{
    public partial class Chemical_Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PreLoadData();
            }
        }

        private void ClearPage()
        {
            tbName.Text = "";
            ddlChemicalGroup.SelectedIndex = 0;
        }

        private void PreLoadData()
        {
            ChemicalGroupBLL bll = new ChemicalGroupBLL();
            ddlChemicalGroup.DataSource=bll.GetAllChemicalGroups();
            ddlChemicalGroup.DataTextField = "Name";
            ddlChemicalGroup.DataValueField = "Id";
            ddlChemicalGroup.DataBind();
        }

        protected void bSave_Click(object sender, EventArgs e)
        {
            ChemicalBLL bll = new ChemicalBLL();
            Chemical chemical = new Chemical();
            try
            {
                chemical.Name = tbName.Text.Trim();
                chemical.ChemicalGroupId = Convert.ToInt32(ddlChemicalGroup.SelectedItem.Value);
                bll.CreateNewChemical(chemical);
                rm.SetSuccessMessage("Add Chemical Successful.");
                ClearPage();
            }
            catch(Exception ex)
            {
                rm.SetFailMessage("Failed To Add Chemical. " + ex.Message);
            }
        }
    }
}